package com.dragon.graduation.projectTeacher.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.dragon.util.MyPage;
import com.dragon.util.WrapperUtil;
import com.dragon.graduation.projectTeacher.controller.dto.ProjectTeacherAddDTO;
import com.dragon.graduation.projectTeacher.controller.dto.ProjectTeacherSearchDTO;
import com.dragon.graduation.projectTeacher.controller.dto.ProjectTeacherUpdateDTO;
import com.dragon.graduation.projectTeacher.controller.vo.ProjectTeacherVO;
import com.dragon.graduation.projectTeacher.entity.ProjectTeacherEntity;
import com.dragon.graduation.projectTeacher.service.ProjectTeacherService;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 题目教师关联表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-03-18 19:45:29
 */
@RestController
@RequestMapping("/projectTeacher")
@RequiredArgsConstructor
@Api(tags = "题目教师关联表 相关接口")
public class ProjectTeacherController {

    private final ProjectTeacherService projectTeacherService;

    @PostMapping("add")
    @ApiOperation("1]题目教师关联表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody ProjectTeacherAddDTO addDTO) {
        ProjectTeacherEntity entity = ProjectTeacherAddDTO.to(addDTO, ProjectTeacherEntity.class);
        projectTeacherService.save(entity);
        return HttpResult.ok("添加成功");
    }

    @PostMapping("update")
    @ApiOperation("2]题目教师关联表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody ProjectTeacherUpdateDTO updateDTO) {
        ProjectTeacherEntity entity = projectTeacherService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = ProjectTeacherUpdateDTO.to(updateDTO, ProjectTeacherEntity.class);
        projectTeacherService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]题目教师关联表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<ProjectTeacherVO>>> search(@RequestBody ProjectTeacherSearchDTO searchDTO) {
        QueryWrapper<ProjectTeacherEntity> queryWrapper = WrapperUtil.constructWrapper(searchDTO, ProjectTeacherEntity.class);

        Page<ProjectTeacherEntity> entityPage = MyPage.setPage(ProjectTeacherEntity.class, searchDTO.getPage());
        Page<ProjectTeacherEntity> page = projectTeacherService.page(entityPage, queryWrapper);
        ResultPage<List<ProjectTeacherVO>> resultPage = MyPage.copyPage(page, ProjectTeacherVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]题目教师关联表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        projectTeacherService.removeByIds(idList);
        return HttpResult.ok("删除成功");
    }
}
